package com.example.petadopt.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.common.Result;
import com.example.petadopt.dao.AdoptDao;
import com.example.petadopt.entity.Adopt;
import com.example.petadopt.ui.adapter.AdoptApplicationAdapter;
import com.example.petadopt.utils.CurrentUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MyApplicationsActivity extends AppCompatActivity {

    private ImageView ivBack;
    private RecyclerView rvApplications;
    private LinearLayout llEmptyView;

    private AdoptApplicationAdapter adapter;
    private List<Adopt> adoptList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_applications);

        initView();
        initListener();
        loadData();
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        rvApplications = findViewById(R.id.rv_applications);
        llEmptyView = findViewById(R.id.ll_empty_view);

        // 设置RecyclerView
        rvApplications.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AdoptApplicationAdapter(adoptList);
        rvApplications.setAdapter(adapter);
    }

    private void initListener() {
        // 返回按钮点击事件
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void loadData() {

        // 获取当前用户ID
        Long userId = CurrentUserUtils.getCurrentUser().getId();

        // 从数据库获取当前用户的领养申请记录
        Result<List<Adopt>> result = AdoptDao.getAdoptsByUserId(userId);

        if (result.isSuccess()) {
            adoptList = result.getData();

            // 更新适配器数据
            adapter.updateData(adoptList);

            // 显示空视图或列表
            if (adoptList == null || adoptList.isEmpty()) {
                rvApplications.setVisibility(View.GONE);
                llEmptyView.setVisibility(View.VISIBLE);
            } else {
                rvApplications.setVisibility(View.VISIBLE);
                llEmptyView.setVisibility(View.GONE);
            }
        } else {
            // 加载失败，显示空视图
            rvApplications.setVisibility(View.GONE);
            llEmptyView.setVisibility(View.VISIBLE);
        }
    }
} 